//
// Created by xwt
//

#ifndef AGENT_COVERAGEPARSE_HPP
#define AGENT_COVERAGEPARSE_HPP

#include "agent_types.hpp"
#include "gcov_coverage_types.hpp"
#include <dirent.h>
#include <string>
#include <vector>
#include <sys/stat.h>
#include <errno.h>


namespace anbantech {
    class coverageParser {
    private:
        std::string mDirectory;

    public:
        std::string readFileToString(const std::string &filename);

        bool hasPrefix(const std::string &s, const std::string &prefix);

        bool fileExist(const std::string &filePath);

        bool validCheck(std::string &name, int lineNumber, std::map<std::string, gcov_function_> &functions);

        std::string getDemangledName(std::string &name, std::map<std::string, gcov_function_> &functions);

        void fillFunctionLines(int count, std::vector<int> &ExecutedCounts);

        void fillUnusedLine(int preLine, int curLine, std::vector<int> &ExecutedCounts);

        std::string trimPrefix(const std::string &s, const std::string &prefix);

        std::string pathJoin(const std::vector<std::string> &elem);

        std::string getExt(const std::string &filename);

        std::string replaceExt(const std::string &path, const std::string &newExt);

        int FindTruePathInCMakeMode(const std::string &dir, const std::string &target,/*out*/std::string &truePath);

        void processFile(const std::string& dir, const std::string& fileName,
                         std::vector<std::string>& lsGcda, std::vector<std::string>& fileNames);

        void walkDir(const std::string& root, const std::string& dir,
                     std::vector<std::string>& lsGcda, std::vector<std::string>& fileNames);
        void sumCov(agent_coverage& x,agent_coverage& cov);
/////////////////////////help funcs end//////////////////////////////////


        std::vector<agent_coverage> Get(const std::string &gcda_root_path);

        int getJsonFileNames(const std::string &dirctory,std::vector<std::string> &gcdaFiles);

        std::vector<agent_coverage> parseCoverageJson(std::string fileName);

        int ParseLocalFileCoverage(gcov_file_ &File, std::string &filePath,
                /*out*/agent_coverage &cov, std::vector<agent_coverage> &functionCovList);
    };

} // anbantech

#endif //AGENT_COVERAGEPARSE_HPP
